package cn.tedu.api;

import java.util.Objects;

/**
 * @author: Dsi
 * @date: 2021/11/12 10:33
 * @description:该类用于测试顶级父类object
 */
public class TestObject {
    public static void main(String[] args) {
        //匿名创建
        new Students(45, "kl");
        Students s = new Students(250, "4");
        Students s2 = new Students(250, "4");
        System.out.println(s.hashCode());
        //测试to.String
        /*Object中的toString(),默认实现：对象的包名.类名@十六进制的哈希值*/
        System.out.println(s.toString());
        //测试equals
        /*==比较的是左右两边的值，1==1，对的；如果是引用类型，比较的是引用类型
        变量保存的地址值，Object中的equals默认是==比较*/
        System.out.println(s.equals(s2));
    }
}

class Students {
    String name;
    int age;
    public Students() {
        System.out.println("我是Students的无参构造");
    }
    public Students(int age, String name) {
        System.out.println("我是Students的含参构造" + age);
        this.name=name;
        this.age=age;
    }
    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Students students = (Students) o;
        return age == students.age && Objects.equals(name, students.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}